require "common/Define"
require "framework/BaseController"
require "FairyGUI"

EmojiMain = BaseController:New({mControllerName = "EmojiMain"});

function EmojiMain:Main()
	-- body
	EmojiMain:Awake();
end

function EmojiMain:Awake()
	-- body
	UIPackage.AddPackage("UI/Emoji");

	UIConfig.verticalScrollBar = "ui://Emoji/ScrollBar_VT";
	UIConfig.defaultScrollBarDisplay = FairyGUI.ScrollBarDisplayType.Auto;

	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown, self);

	-- load ui
	local prePackageName = "UI/";
	local packageName = "Emoji";
	UIPackage.AddPackage(prePackageName..packageName);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject(packageName, componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	print(self)
	self:OnViewCreated(self.mainComponent);

	self.intItemCount = 0;
end

function EmojiMain:InitView()
	-- body
	self._messages = {};
	self._list = self.mainComponent:GetChild("list").asList;
	self._list:SetVirtual();
	self._list.itemProvider = self.GetListItemResource;
	self._list.itemRenderer = self.RenderListItem;
	print("itemRenderer");
	print(self._list.itemRenderer);

	self._input1 = self.mainComponent:GetChild("input1").asTextInput;
	self._input1.onKeyDown:Add(self.__inputKeyDown1);

	self._input2 = self.mainComponent:GetChild("input2").asTextInput;
	self._input2.onKeyDown:Add(self.__inputKeyDown2);

	-- add some emoji to input text
	-- self._emojies = CS.System.Collections.Generic["Dictionary`2[System.UInt32,FairyGUI.Emoji]"](); -- ()Dictionary<uint, Emoji>();
	self._emojies = System.Collections.Generic.Dictionary_uint_FairyGUI_Emoji.New();
	for i = 0x1f600,0x1f637, 1 do
		local url = UIPackage.GetItemURL("Emoji", System.Convert.ToString(i, 16));
		if url ~= nil then
			self._emojies:Add(i, Emoji.New(url));
		end
	end

	self._input2.emojies = self._emojies;

	self.mainComponent:GetChild("btnSend1").onClick:Add(self.__clickSendBtn1);
	self.mainComponent:GetChild("btnSend2").onClick:Add(self.__clickSendBtn2);

	self.mainComponent:GetChild("btnEmoji1").onClick:Add(self.__clickEmojiBtn1);
	self.mainComponent:GetChild("btnEmoji2").onClick:Add(self.__clickEmojiBtn2);

	self._emojiSelectUI1 = UIPackage.CreateObject("Emoji", "EmojiSelectUI").asCom;
	self._emojiSelectUI1.fairyBatching = true;
	self._emojiSelectUI1:GetChild("list").asList.onClickItem:Add(self.__clickEmoji1);

	self._emojiSelectUI2 = UIPackage.CreateObject("Emoji", "EmojiSelectUI_ios").asCom;
	self._emojiSelectUI2.fairyBatching = true;
	self._emojiSelectUI2:GetChild("list").asList.onClickItem:Add(self.__clickEmoji2);
end

function EmojiMain.GetListItemResource(index)
	-- body
	print("GetListItemResource");
	
	local msg = EmojiMain._messages[index+1];
	if msg.fromMe then
		return "ui://Emoji/chatRight";
	else
		return "ui://Emoji/chatLeft";
	end
end

function EmojiMain.RenderListItem(index, obj)
	print("RenderListItem");

	local item = obj;
	local msg = EmojiMain._messages[index+1];
	if msg.fromeMe == false then
		item:GetChild("name").text = msg.sender;
	end
	item.icon = UIPackage.GetItemURL("Emoji", msg.senderIcon);

	local tf = item:GetChild("msg").asRichTextField;
	tf.emojies = EmojiMain._emojies;
	tf.width = tf.initWidth;
	tf.text = EmojiParser.inst:Parse(msg.msg);
	tf.width = tf.textWidth;
end

function EmojiMain.__clickSendBtn1(context)
	local msg = EmojiMain._input1.text;
	if msg.Length == 0 then
		return;
	end

	print("__clickSendBtn1");
	EmojiMain.AddMsg("Unity", "r0", msg, true);
	EmojiMain._input1.text = "";
end
function EmojiMain.__clickSendBtn2(context)
	local msg = EmojiMain._input2.text;
	if msg.Length == 0 then
		return;
	end

	EmojiMain.AddMsg("Unity", "r0", msg, true);
	EmojiMain._input2.text = "";
end
function EmojiMain.__clickEmojiBtn1(context)
	GRoot.inst:ShowPopup(EmojiMain._emojiSelectUI1, context.sender, false);
end
function EmojiMain.__clickEmojiBtn2(context)
	GRoot.inst:ShowPopup(EmojiMain._emojiSelectUI2, context.sender, false);
end

function EmojiMain.__inputKeyDown1(context)
	-- body
	if context.inputEvent.keyCode == UnityEngine.KeyCode.Return then
		self.mainComponent:GetChild("btnSend1").onClick:Call();
	end
end

function EmojiMain.__inputKeyDown2(context)
	-- body
	if (context.inputEvent.keyCode == UnityEngine.KeyCode.Return) then
		self.mainComponent:GetChild("btnSend2").onClick:Call();
	end
end

function EmojiMain:OnKeyDown(context)
 	-- body
 	if (context.inputEvent.keyCode == UnityEngine.KeyCode.Escape) then
		print("Exit Game!");
		Application.Quit();
	end
end

function EmojiMain.AddMsg(sender, senderIcon, msg, fromMe)
	local isScrollBottom = EmojiMain._list.scrollPane.isBottomMost;
	print("AddMsg: sender="..sender.." senderIcon="..senderIcon.." msg="..msg);

	local newMessage = {};
	newMessage.sender = sender;
	newMessage.senderIcon = senderIcon;
	newMessage.msg = msg;
	newMessage.fromMe = fromMe;
	table.insert(EmojiMain._messages, newMessage);
	EmojiMain.intItemCount = EmojiMain.intItemCount + 1;
	for i,v in ipairs(EmojiMain._messages) do  print("value="..v.msg) end   

	if (newMessage.fromMe) then
	
		math.randomseed(tostring(os.time()):reverse():sub(1, 6));
		if (EmojiMain._messages.Count == 1 or math.random(0, 1.0) < 0.5) then
			local replyMessage = {};
			replyMessage.sender = "FairyGUI";
			replyMessage.senderIcon = "r1";
			replyMessage.msg = "Today is a good day." --" \U0001f600";
			replyMessage.fromMe = false;
			table.insert(EmojiMain._messages, replyMessage);
			EmojiMain.intItemCount = EmojiMain.intItemCount + 1;

			print("add another msg.");
			for i,v in ipairs(EmojiMain._messages) do  print("value="..v.msg) end   
		end

	end

	--if (EmojiMain._messages.Count > 100)
		--EmojiMain._messages.RemoveRange(0, _messages.Count - 100);

	print(table.maxn);
	EmojiMain._list.numItems = EmojiMain.intItemCount;

	if (isScrollBottom) then
		EmojiMain._list.scrollPane:ScrollBottom();
	end
end

function EmojiMain.__clickEmoji1(context)
	local item = context.data;
	EmojiMain._input1:ReplaceSelection("[:"..item.text.."]");
end

function EmojiMain.__clickEmoji2(context)
	
	local item = context.data;
	print("__clickEmoji2", item, item.icon);
	local tt = UIPackage.GetItemByURL(item.icon);
	local tName = tt.name;
	local tChar = System.Convert.ToInt32(tName, 16);
	local tValue = FairyGUI.LuaUIHelper.ConvertFromUtf32(tChar);
	print(tName," ", tChar, " ", tValue);
	EmojiMain._input2:ReplaceSelection( tValue );
end